<?php
//二级分类自定义函数
function order ($array,$pid=0){
    $arr = array();
    foreach($array as $v){
        if($v['pid']==$pid){
            $arr[] = $v;
        }
        for($i=0;$i<count($arr);$i++){
            if($arr[$i]['id'] == $v['pid']){
                $arr[$i]['area'][$v['id']] = $v;
            }
            //$arr = array_merge($arr,order($array,$v['id']));递归
        }
    }
    return $arr;
}
function url_base64_encode($str)
{
    $code = base64_encode($str);        
    $code = str_replace('+',"!",$code); //把所用"+"替换成"!"
    $code = str_replace('/',"*",$code); //把所用"/"替换成"*"
    $code = str_replace('=',"",$code);  //把所用"="删除掉
    return $code;
}

function url_base64_decode($code)
{
    $code = str_replace("!",'+',$code);//把所用"+"替换成"!"
    $code = str_replace("*",'/',$code);//把所用"/"替换成"*"
    $str  = base64_decode($code);
    return $str;
}

function get_region($code)
{
	$map['code'] = $code;
	$return = M('region')->where($map)->find();
	if(empty($return)){
		return '未知位置';
	}else{
		return $return['fullname'];
	}
}
//将字符串拆分成一维数组
function strsToArray($strs) {
    $result = array();
    $array = array();
    $strs = str_replace('，', ',', $strs);
    $strs = str_replace("n", ',', $strs);
    $strs = str_replace("rn", ',', $strs);
    $strs = str_replace(' ', ',', $strs);
    $array = explode(',', $strs);
    foreach ($array as $key => $value) {
        if ('' != ($value = trim($value))) {
            $result[] = $value;
        }
    }
    return $result;
}

//将字符串拆分成一维数组
function strsToArrays($strs) {
    $result = array();
    $array = array();
    $strs = str_replace('/', '/', $strs);
    $strs = str_replace("n", '/', $strs);
    $strs = str_replace("rn", '/', $strs);
    $strs = str_replace(' ', '/', $strs);
    $array = explode('/', $strs);
    foreach ($array as $key => $value) {
        if ('' != ($value = trim($value))) {
            $result[] = $value;
        }
    }
    return $result;
}

/**
 * 格式化打印数组
 * @param  [type] $arr [description]
 * @return [type] $arr [description]
 */
function p ($arr)
{
    echo '<pre>';
    print_r($arr);
    echo '</pre>';
}

//日志函数
function logResults($word='') {
    $fp = fopen("log.txt","a");
    flock($fp, LOCK_EX) ;
    fwrite($fp,"执行日期：".strftime("%Y%m%d%H%M%S",time())."\n".$word."\n");
    flock($fp, LOCK_UN);
    fclose($fp);
}
//将秒转换成 年-天-时-分-秒
function Sec2Time($time){
    if(is_numeric($time)){
        $value = array(
            "years" => 0, "days" => 0, "hours" => 0,
            "minutes" => 0, "seconds" => 0,
        );
        //年
        if($time >= 31556926){
            $value["years"] = floor($time/31556926);
            $time = ($time%31556926);
        }
        //天
        if($time >= 86400){
            $value["days"] = floor($time/86400);
            $time = ($time%86400);
        }
        //小时
        if($time >= 3600){
            $value["hours"] = floor($time/3600);
            $time = ($time%3600);
        }
        //分
        if($time >= 60){
            $value["minutes"] = floor($time/60);
            $time = ($time%60);
        }
        $value["seconds"] = floor($time);
        $t=$value["years"] ."年". $value["days"] ."天".$value["hours"] ."小时". $value["minutes"] ."分".$value["seconds"]."秒";
        Return $t;

    }else{
        return (bool) FALSE;
    }
}
//将秒转换成 天-时-分-秒
function Sec2Timeday($time){
    if(is_numeric($time)){
        $value = array(
            "days" => 0, "hours" => 0,
            "minutes" => 0, "seconds" => 0,
        );
        //天
        if($time >= 86400){
            $value["days"] = floor($time/86400);
            $time = ($time%86400);
        }
        //小时
        if($time >= 3600){
            $value["hours"] = floor($time/3600);
            $time = ($time%3600);
        }
        //分
        if($time >= 60){
            $value["minutes"] = floor($time/60);
            $time = ($time%60);
        }
        $value["seconds"] = floor($time);
        $t=$value["days"] ."天".$value["hours"] ."小时". $value["minutes"] ."分".$value["seconds"]."秒";
        Return $t;

    }else{
        return (bool) FALSE;
    }
}
//将秒转换成 时-分-秒
function Sec2Times($time){
    if(is_numeric($time)){
        $value = array(
            "hours"   => 0,
            "minutes" => 0,
            "seconds" => 0,
        );
        //小时
        if($time >= 3600){
            $value["hours"] = floor($time/3600);
            $time = ($time%3600);
        }
        //分
        if($time >= 60){
            $value["minutes"] = floor($time/60);
            $time = ($time%60);
        }
        $value["seconds"] = floor($time);
        $t=$value["hours"] ."小时". $value["minutes"] ."分".$value["seconds"]."秒";
        Return $t;

    }else{
        return (bool) FALSE;
    }
}

